package com.itheima.tliaswebmanagement.controller;

import com.itheima.tliaswebmanagement.annotation.OperationLog;
import com.itheima.tliaswebmanagement.pojo.Dept;
import com.itheima.tliaswebmanagement.pojo.Result;
import com.itheima.tliaswebmanagement.service.DeptService;
import com.itheima.tliaswebmanagement.service.impl.ClazzServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {
    @Autowired
    private DeptService deptService;

    @GetMapping
    public Result findAll() {
        List<Dept> depts = deptService.finAll();
        return Result.success(depts);
    }

    @OperationLog
    @DeleteMapping
    //请求参数是怎么确定出来的？？？findById同样是根据id进行查询的，为什么删除就不用加/{id}？
    //deleteById的请求形式是/depts?id=1（查询字符串），直接传id即可
    //findById的请求形式是/depts/1(路径参数)，需要添加@PathVariable和“/{id}”
    public Result deleteById(Integer id) {
        System.out.println(id);
        deptService.deleteById(id);
        return Result.success();
    }

    @OperationLog
    @PostMapping
    //接收的是一个json格式的数据
    //@RequestBody：将JSON格式的数据转换成Dept对象
    public Result save(@RequestBody Dept dept) {
        System.out.println(dept);
        deptService.save(dept);
        return Result.success();
    }

    //{}大括号中的名字和Integer类型的id一致，因此可以写成@PathVariable
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        log.debug("{}",id);
//        System.out.println(id);
        Dept dept = deptService.findById(id);
        return Result.success(dept);
    }

    @OperationLog
    @PutMapping
    public Result update(@RequestBody Dept dept){
        deptService.update(dept);
        return Result.success();
    }

}
